<template>
  <div id="login" :style="back">
    <div id="center">
      <div class="c-box">
        <div class="c-box-box">
          <img src="../../assets/logo.png" alt />
        </div>
      </div>
      <section>
        <el-form
          label-width="100px"
          class="demo-ruleForm"
          :rules="rules"
          :model="ruleForm"
          ref="ruleForm"
        >
          <el-form-item prop="username">
            <el-input
              style="padding:'0 40px'"
              placeholder="请输入你的用户名"
              v-model="ruleForm.username"
              prefix-icon="el-icon-user-solid"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入你的密码"
              v-model="ruleForm.password"
              type="password"
              prefix-icon="el-icon-s-order"
            ></el-input>
          </el-form-item>
          <el-form-item class="right">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetForm(ruleForm)">重置</el-button>
          </el-form-item>
        </el-form>
      </section>
    </div>
  </div>
</template> 
<script>
import { getLogin } from "@/api/Login/login.js";
export default {
  data() {
    return {
      //登录页面背景图
      back: {
        backgroundImage:
          "url(" +
          require("../../assets/u=1397650868,3923351836&fm=26&gp=0.jpg") +
          ")",
        backgroundRepeat: "no-repeat",
        backgroundSize: "100% 100%"
      },
      ruleForm: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          { required: true, message: "请输入正确的用户名", trigger: "blur" },
          { min: 4, max: 8, message: "长度在 4 到 8 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入正确的密码", trigger: "blur" },
          { min: 4, max: 8, message: "长度在 4 到 8 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate(async valid => {
        //   console.log(valid) true
        if (valid) {
          let{data:res}= await getLogin(this.ruleForm)
            console.log(res);
            if (res.meta.msg == "登录成功") {
              sessionStorage.setItem("loginmsg", JSON.stringify(res.data)); //把数据存储到本地
              this.$message({
                message: "恭喜你,登录成功",
                type: "success"
              });
              this.$router.push("/home");
            } else {
              this.$message({
                message: "登录失败",
                type: "error"
              });
            }
          }
        
      });
    },
    resetForm(ruleForm) {
      this.$refs.ruleForm.resetFields();
      console.log(this.ruleForm);
    }
  }
}
</script>
<style lang="scss" scoped>
#login {
  width: 100%;
  height: 100%;
  #center {
    position: absolute;
    right: 0;
    top: 60%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 10px #ddd;
    width: 450px;
    height: 304px;
    border-radius: 3px;
    background: rgba(0, 0, 0, 0.5);
    .c-box {
      position: absolute;
      top: -25%;
      left: 33%;
      width: 130px;
      height: 130px;
      background: rgba(0, 0, 0, 0.5);
      padding: 10px;
      border: 1px solid #efefef;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;

      .c-box-box {
        width: 110px;
        height: 110px;
        padding: 10px;
        background: #cccccc;
        border: 1px solid #cccccc;
        border-radius: 50%;
        img {
          width: 100%;
          height: 100%;
          border-radius: 40%;
        }
      }
    }
    section {
      padding: 20px;
      padding-top: 100px;
    }
  }
}
.right {
  float: right;
}
/deep/.el-form-item__content {
  margin-left: 0 !important;
  padding: 0 40px !important;
}
/deep/.el-form-item__error {
  padding: 4px 40px !important;
}
</style>

